﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using Model;

namespace BusinessLogic
{
    public class MaintainTransaction : BaseCommand<object>
    {

        acm_tx tx;


        public MaintainTransaction(acm_tx trx)
        {
            tx = trx;

            StringBuilder sb = new StringBuilder();

            if (tx.id == null || tx.id == 0)
            {                
                sb.Append("INSERT INTO acm_tx([sales_id],[vin],[campaign_id_adv],[campaign_id_dlv],[car_series_id],");
                sb.Append("[adv_date],[dlv_date] ,[adv_promotion_amount],[dlv_promotion_amount],[diff_promotion_amount],");
                sb.Append("[attach1] ,[tx_status] ,[tx_date] ,[attach1_date],");
                sb.Append("[tx_dealer_code],[tx_branch_code],");
                sb.Append("[addr_no],[village],[group_no],[alley],[building_name],");
                sb.Append("[room_no],[floor_no] ,[road_name] ,[parish] ,[district] ,");
                sb.Append("[province] ,[postcode] ,[customer_name] ,[id_card], [mobile_note4], [passport], [campaign_with_note4]) VALUES (");
                
                sb.Append("@sales_id,@vin,@campaign_id_adv,@campaign_id_dlv,@car_series_id,");
                sb.Append("@adv_date,@dlv_date,@adv_promotion_amount,@dlv_promotion_amount,@diff_promotion_amount,");
                sb.Append("@attach1,@tx_status,@tx_date,@attach1_date,");
                sb.Append("@tx_dealer_code,@tx_branch_code,");
                sb.Append("@addr_no,@village,@group_no,@alley,@building_name,");
                sb.Append("@room_no,@floor_no,@road_name,@parish,@district,");
                sb.Append("@province,@postcode,@customer_name,@id_card,@mobile_note4,@passport, @campaign_with_note4)");

                SetQuery(sb.ToString());
            }
            else
            {
                sb.Append("UPDATE acm_tx SET [sales_id]=@sales_id,[vin]=@vin,[campaign_id_adv]=@campaign_id_adv,[campaign_id_dlv]=@campaign_id_dlv,[car_series_id]=@car_series_id,");
                sb.Append("[adv_date]=@adv_date,[dlv_date]=@dlv_date ,[adv_promotion_amount]=@adv_promotion_amount,[dlv_promotion_amount]=@dlv_promotion_amount,[diff_promotion_amount]=@diff_promotion_amount,");
                if (tx.attach1 != null && tx.attach1 != "")
                {
                    sb.Append("[attach1]=@attach1 ,[attach1_date]=@attach1_date,");
                }

                sb.Append("[tx_status]=@tx_status ,[tx_date]=@tx_date ,");
                sb.Append("[tx_dealer_code]=@tx_dealer_code,[tx_branch_code]=@tx_branch_code,");
                sb.Append("[addr_no]=@addr_no,[village]=@village,[group_no]=@group_no,[alley]=@alley,[building_name]=@building_name,");
                sb.Append("[room_no]=@room_no,[floor_no]=@floor_no ,[road_name]=@road_name ,[parish]=@parish ,[district]=@district ,");
                sb.Append("[province]=@province ,[postcode]=@postcode ,[customer_name]=@customer_name ,[id_card]=@id_card,");
                sb.Append("[mobile_note4]=@mobile_note4, [passport]=@passport, [campaign_with_note4]=@campaign_with_note4 WHERE id=@id");

                SetQuery(sb.ToString());

                cmd.Parameters.AddWithValue("id",tx.id);
            }

                
            cmd.Parameters.AddWithValue("sales_id",tx.sales_id);
            cmd.Parameters.AddWithValue("vin",tx.vin);
            cmd.Parameters.AddWithValue("campaign_id_adv",tx.campaign_id_adv != null ? tx.campaign_id_adv : 0);
            cmd.Parameters.AddWithValue("campaign_id_dlv", tx.campaign_id_dlv != null ? tx.campaign_id_dlv : 0);
            cmd.Parameters.AddWithValue("car_series_id",tx.car_series_id);

            cmd.Parameters.AddWithValue("adv_date",tx.adv_date);
            cmd.Parameters.AddWithValue("dlv_date",tx.dlv_date);
            cmd.Parameters.AddWithValue("adv_promotion_amount",tx.adv_promotion_amount);
            cmd.Parameters.AddWithValue("dlv_promotion_amount",tx.dlv_promotion_amount);
            cmd.Parameters.AddWithValue("diff_promotion_amount",(tx.diff_promotion_amount != null) ? tx.diff_promotion_amount.Value : 0);

            if (tx.attach1 != null && tx.attach1 != "")
            {
                cmd.Parameters.AddWithValue("attach1", tx.attach1);
                cmd.Parameters.AddWithValue("attach1_date", tx.attach1_date);
            }

            cmd.Parameters.AddWithValue("tx_status",tx.tx_status);
            cmd.Parameters.AddWithValue("tx_date",tx.tx_date);

            cmd.Parameters.AddWithValue("tx_dealer_code",tx.tx_dealer_code);
            cmd.Parameters.AddWithValue("tx_branch_code",(tx.tx_branch_code != null) ? tx.tx_branch_code : "");

            cmd.Parameters.AddWithValue("addr_no",tx.addr_no);
            cmd.Parameters.AddWithValue("village",tx.village);
            cmd.Parameters.AddWithValue("group_no",tx.group_no);
            cmd.Parameters.AddWithValue("alley",tx.alley);
            cmd.Parameters.AddWithValue("building_name",tx.building_name);

            cmd.Parameters.AddWithValue("room_no",tx.room_no);
            cmd.Parameters.AddWithValue("floor_no",tx.floor_no);
            cmd.Parameters.AddWithValue("road_name",tx.road_name);
            cmd.Parameters.AddWithValue("parish",tx.parish);
            cmd.Parameters.AddWithValue("district",tx.district);

            cmd.Parameters.AddWithValue("province",tx.province);
            cmd.Parameters.AddWithValue("postcode",tx.postcode);
            cmd.Parameters.AddWithValue("customer_name",tx.customer_name);
            cmd.Parameters.AddWithValue("id_card",tx.id_card);
            cmd.Parameters.AddWithValue("mobile_note4", (tx.mobile_note4 != null) ? tx.mobile_note4.Value : 0);
            cmd.Parameters.AddWithValue("passport", tx.passport);
            cmd.Parameters.AddWithValue("campaign_with_note4", tx.campaign_with_note4);
        }


        public override void Command()
        {
            Result = cmd.ExecuteNonQuery();
            if (Convert.ToInt32(Result) > 0)
            {
                if (tx.id == 0)
                {
                    SqlCommand cmd2 = conn.CreateCommand();
                    cmd2.CommandText = "SELECT IDENT_CURRENT('acm_tx')";
                    SqlDataReader dr = cmd2.ExecuteReader();
                    dr.Read();
                    Result = dr[0];
                }
                else Result = tx.id;
            }
        }
    }
}



//  id, sales_id, vin, campaign_id_adv, campaign_id_dlv, car_series_id, adv_date, dlv_date, adv_promotion_amount,
//  dlv_promotion_amount, diff_promotion_amount, attach1, tx_status, tx_date, attach1_date, attach1_view_date,
//  tx_dealer_code, tx_branch_code, change_from_dealer_code, change_from_branch_code, change_request_status,
//  change_request_date, change_reply_date, change_reply_by,  warranty_print_date, warran_print_by, addr_no,
//  village, group_no, alley, building_name, room_no, floor_no, road_name, parish, district, province, postcode,
//  customer_name, id_card


// StringBuilder sb = new StringBuilder();
//sb.Append("INSERT INTO acm_tx([sales_id],[vin],[campaign_id_adv],[campaign_id_dlv],[car_series_id],");
//sb.Append("[adv_date],[dlv_date] ,[adv_promotion_amount],[dlv_promotion_amount],[diff_promotion_amount],");
//sb.Append("[attach1] ,[tx_status] ,[tx_date] ,[attach1_date]  ,[attach1_view_date] ,");
//sb.Append("[tx_dealer_code],[tx_branch_code],[change_from_dealer_code],[change_from_branch_code],[change_request_status],");
//sb.Append("[change_request_date],[change_reply_date],[change_reply_by],[warranty_print_date],[warran_print_by],");
//sb.Append("[addr_no],[village],[group_no],[alley],[building_name],")
//sb.Append("[room_no],[floor_no] ,[road_name] ,[parish] ,[district] ,");
//sb.Append("[province] ,[postcode] ,[customer_name] ,[id_card]) VALUES (");
//sb.Append("@sales_id,@vin,@campaign_id_adv,@campaign_id_dlv,@car_series_id,");
//sb.Append("@adv_date,@dlv_date,@adv_promotion_amount,@dlv_promotion_amount,@diff_promotion_amount,");
//sb.Append("@attach1,@tx_status,@tx_date,@attach1_date,@attach1_view_date, datetime,");
//sb.Append("@tx_dealer_code,@tx_branch_code,@change_from_dealer_code,@change_from_branch_code,@change_request_status,");
//sb.Append("@change_request_date,@change_reply_date,@change_reply_by,@warranty_print_date,@warran_print_by,");
//sb.Append("@addr_no,@village,@group_no,@alley,@building_name,");
//sb.Append("@room_no,@floor_no,@road_name,@parish,@district,");
//sb.Append("@province,@postcode,@customer_name,@id_card)");